package ifes;

import java.io.*;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


/*
    Cataloga os autores e as editoras do CetenFolha
 */


public class CetenFolha {


    static String pathBd = "";
    static String pathExt = "";
    static ArrayList<String> exts = new ArrayList<>();
    static HashMap<String, ArrayList<String>> autores = new HashMap<String, ArrayList<String>>();
    static HashMap<String, ArrayList<String>> cadernos = new HashMap<String, ArrayList<String>>();

    public static void setPath(String valor) {

        pathBd = valor;

    }

    public static void indexa(String valor) throws IOException {

        pathExt = valor;
        File dir = new File(pathExt + "\\CETENFolhaNDX");
        File dir2 = new File(pathExt + "\\CETENFolhaNDX\\corpus sentença");
        File dir3 = new File(pathExt + "\\CETENFolhaNDX\\corpus sentença\\corpus documentos");
        try {
            dir.mkdir();
            dir2.mkdir();
            dir3.mkdir();
        } catch (Exception ex) {
            System.out.println("erro");
        }

        try {
            FileReader reader = new FileReader(pathBd);
            BufferedReader leitor = new BufferedReader(reader);
            int i = 0;
            String ext = "";
            while (leitor.ready()) {
                String linha = leitor.readLine();
                int pos = linha.indexOf("<ext");
                ext = "";
                if (pos != -1) {
                    ext += linha;
                    linha = leitor.readLine();
                    int pos2 = linha.indexOf("ext>");
                    while (pos2 == -1) {
                        linha = leitor.readLine();
                        ext += linha;
                        pos2 = linha.indexOf("ext>");
                    }

                    exts.add(ext);
                }

            }
            float gambit = (float) 0.000001;
            for (int j = 0; j < exts.size(); j++) {
                FileWriter file = new FileWriter(pathExt + "\\CETENFolhaNDX\\corpus sentença\\corpus documentos\\" + "ext" + new DecimalFormat("#.000000").format(gambit).replace(",", "") + ".txt");

                BufferedWriter out = new BufferedWriter(file);
                out.write(exts.get(j));
                out.close();

                gambit += 0.000001;
            }

            String[] arr_split;
            String[] arr_split2;
            String caderno;
            String autor;

            gambit = (float) 0.000001;
            for (int j = 0; j < exts.size(); j++) {
                arr_split = exts.get(j).split("cad=");
                if (arr_split.length > 1) {
                    arr_split2 = arr_split[1].split("\"");
                    caderno = arr_split2[1].substring(0, arr_split2[1].length());
                    caderno = caderno.replaceAll("[^\\w&&[^á^é^í^ó^ú^Á^É^Í^Ó^Ú^â^ê^ô^â^ô^ ]]", "");
                    if (cadernos.get(caderno) == null) {
                        cadernos.put(caderno, new ArrayList<String>());
                    }
                    cadernos.get(caderno).add(new DecimalFormat("#.000000").format(gambit).replace(",", "") + ".txt");
                }

                gambit += 0.000001;

            }

            gambit = (float) 0.000001;
            for (int j = 0; j < exts.size(); j++) {
                arr_split = exts.get(j).split("<a>");
                if (arr_split.length > 1) {
                    arr_split2 = arr_split[1].split("<");
                    autor = arr_split2[0].substring(1, arr_split2[0].length() - 1);
                    autor = autor.replaceAll("[^\\w&&[^á^é^í^ó^ú^Á^É^Í^Ó^Ú^â^ê^ô^â^ô^ ]]", "");
                    if (autores.get(autor) == null) {
                        autores.put(autor, new ArrayList<String>());
                    }
                    autores.get(autor).add(new DecimalFormat("#.000000").format(gambit).replace(",", "") + ".txt");
                }

                gambit += 0.000001;

            }

            String file = pathExt + "\\CETENFolhaNDX\\corpus sentença\\cetenfolha-autor.csv";
            FileWriter writer = new FileWriter(file);

            for (Map.Entry<String, ArrayList<String>> entry : autores.entrySet()) {
                String string = entry.getKey();
                ArrayList<String> arrayList = entry.getValue();

                writer.append(entry.getKey());

                for (i = 0; i < entry.getValue().size(); i++) {

                    writer.append(',');
                    writer.append(entry.getValue().get(i));

                }
                writer.append('\n');

            }
            writer.flush();
            writer.close();

            String file2 = pathExt + "\\CETENFolhaNDX\\corpus sentença\\cetenfolha-caderno.csv";
            FileWriter writer2 = new FileWriter(file2);
            writer2.append("Caderno");
            writer2.append(',');
            writer2.append("Arquivo");
            writer2.append('\n');

            for (Map.Entry<String, ArrayList<String>> entry : cadernos.entrySet()) {
                String string = entry.getKey();
                ArrayList<String> arrayList = entry.getValue();


                writer2.append(entry.getKey());

                for (i = 0; i < entry.getValue().size(); i++) {

                    writer2.append(',');
                    writer2.append(entry.getValue().get(i));

                }
                writer2.append('\n');

            }

            writer2.flush();
            writer2.close();

        } catch (Exception ex) {
            System.out.println("erro");
        }
    }

    public static void main(String[] args) {
        setPath("C:\\Users\\Cássio\\Downloads\\CETENFolha-1.0");
        try {
            indexa("C:\\");


        } catch (IOException ex) {
            ex.printStackTrace();
        }


    }
}
